package jt.classic.app.info;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;
import jt.classic.system.utils.AnnexTool;
import jt.classic.system.wordbook.XWordbookMain;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.SimpleBean;
import org.limp.basework.console.XSystemConsole;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBeanImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.Controller;
import org.limp.mine.DateTrimmer;
import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;

import java.sql.Connection;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 新闻评论审核控制器
 *
 * @author mido
 *
 */
public class InfoReplySHAction extends SimpleAction {
    /**
     * 新闻评论信息列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String qname = StringTool.checkString(param.get("qname"));
        String qtype = StringTool.checkString(param.get("qtype"));
        String qdirec = StringTool.checkString(param.get("qdirec"));
        String qdirecid = StringTool.checkString(param.get("qdirecid"));
        String qstatus = StringTool.checkString(param.get("qstatus"));
        String qdate = StringTool.checkString(param.get("qdate"));

        String listtype = StringTool.checkString(param.get("listtype"));

        if (listtype.equals("")) {
            listtype = "type&directory";
        }

        request.setAttribute("listtype", listtype);

        StringBuffer where = new StringBuffer(
                " WHERE APP_INFO.IF_ID = APP_INFO_RECEIPT.AF_ID AND IF_DEL = 0 ");

        if (listtype.equals("directory")) {
            where.append(" AND ID_ID <> 0 ");
        } else if (listtype.equals("type")) {
            where.append(" AND ID_ID = 0 ");
        }

        if (!qname.equals("")) {
            where.append(" AND IF_TITLE LIKE '%" + qname + "%' ");
        }

        if (!qtype.equals("")) {
            where.append(" AND IT_NAME = '" + qtype + "' ");
        }

        if (!qdirecid.equals("")) {
            where.append(" AND ID_ID = '" + qdirecid + "' ");
        }

        if (!qstatus.equals("")) {
            where.append(" AND RSTATUS = " + qstatus + " ");
        }

        if (!qdate.equals("")) {
            where.append(" AND IF_DATE LIKE '%" +
                DateTrimmer.formatDateForVarchar(qdate) + "%' ");
        }

        if (!ISystem.matchrole(user, "cmsinforeplyauditor")) {
            where.append(" AND 1 = -1 ");
        }

        Map query = new HashMap();
        query.put("qname", qname);
        query.put("qtype", qtype);
        query.put("qdirec", qdirec);
        query.put("qdirecid", qdirecid);
        query.put("qstatus", qstatus);
        query.put("qdate", qdate);

        request.setAttribute("query", query);

        String sql1 = "SELECT COUNT(APP_INFO.IF_ID) FROM APP_INFO, APP_INFO_RECEIPT " +
            where.toString();
        String sql2 = "SELECT APP_INFO_RECEIPT.*,APP_INFO.IF_ID,APP_INFO.IF_TITLE  FROM APP_INFO, APP_INFO_RECEIPT " +
            where.toString() + " ORDER BY RTIME DESC";

        String url = request.getContextPath() +
            "/app/inforeplysh.do?method=list";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance()
                                             .createQueryJack(false);

            // 设置
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            // 获得跳转控制
            Controller controller = plus.getController("form1");

            // 设置调转类型
            controller.setType("input");

            // 显示标签
            Label label = plus.getLabel();

            // 设置显示标签类型
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;

            request.setAttribute("result", result);

            XWordbookMain wbmain = XWordbookMain.getInstance();

            List list = wbmain.getWord("info-type");

            request.setAttribute("list", list);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("list");
    }

    /**
    * 审核通过
    *
    * @param mapping
    * @param form
    * @param request
    * @param response
    * @return
    * @throws Exception
    */
    public ActionForward approve(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        String ids = StringTool.checkString(request.getParameter("ids"));
        String ifids = StringTool.checkString(request.getParameter("ifids"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            BaseworkUtil util = new BaseworkUtil();

            String[] idstr = ids.split(",");
            String[] ifidstr = ifids.split(",");

            SimpleBean sb = new SimpleBeanImpl();

            InfoManager manager = new InfoManager(conn);
            
            for (int i = 0; i < idstr.length; i++) {
                sb.getResource().put("pkid", idstr[i]);
                sb.getResource().put("rstatus", "1");

                util.manualUpdate("APP_INFO_RECEIPT", sb, conn);
                
                manager.updateReplyCount(ifidstr[i], 1);
            }

            conn.commit();

            request.setAttribute("result", "{success: true}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 审核未通过
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward reject(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        String ids = StringTool.checkString(request.getParameter("ids"));
        String ifids = StringTool.checkString(request.getParameter("ifids"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            BaseworkUtil util = new BaseworkUtil();

            String[] idstr = ids.split(",");
            String[] ifidstr = ifids.split(",");

            SimpleBean sb = new SimpleBeanImpl();

            InfoManager manager = new InfoManager(conn);
            
            for (int i = 0; i < idstr.length; i++) {
                sb.getResource().put("pkid", idstr[i]);
                sb.getResource().put("rstatus", "-1");

                util.manualUpdate("APP_INFO_RECEIPT", sb, conn);
                
                manager.updateReplyCount(ifidstr[i], -1);
            }

            conn.commit();

            request.setAttribute("result", "{success: true}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 删除
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;
        
        String ids = StringTool.checkString(request.getParameter("ids"));
        String ifids = StringTool.checkString(request.getParameter("ifids"));
        String status = StringTool.checkString(request.getParameter("status"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            BaseworkUtil util = new BaseworkUtil();

            String[] idstr = ids.split(",");
            String[] ifidstr = ifids.split(",");
            String[] statusstr = status.split(",");

            SimpleBean sb = new SimpleBeanImpl();

            InfoManager manager = new InfoManager(conn);
            
            for (int i = 0; i < idstr.length; i++) {
                sb.getResource().put("pkid", idstr[i]);

                util.manualDelete("APP_INFO_RECEIPT", sb, conn);
                
                if(statusstr[i].equals("1")){
                	manager.updateReplyCount(ifidstr[i], -1);
                }
            }

            conn.commit();

            String url = "/inforeplysh.do?method=list";
            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }
}
